﻿using IntelligentAgriculture.Domain.Entity;
using IntelligentAgriculture.Domain.Interface;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IntelligentAgriculture.Domain.Impl
{
	/// <summary>
	/// 羊只采购显示
	/// </summary>
	public class PurchaseFormModelRepository:IPurchaseFormModelRepository
	{
		/// <summary>
		/// 上下文
		/// </summary>
		private readonly AgricultureDbContext _context;
		/// <summary>
		/// 构造函数
		/// </summary>
		/// <param name="context"></param>

		public PurchaseFormModelRepository(AgricultureDbContext context)
		{
			_context = context;
		}
		/// <summary>
		/// 获取采购列表
		/// </summary>
		/// <returns></returns>
		public async Task<IQueryable<Purchaseformmodel>> GetPurchaseForms()
		{
			//var list = from a in _context.Purchaseformmodels
			//		   join b in _context.Vendormodels
			//		   on a.VendorSnowId equals b.VendorSnowId
			//		   join c in _context.Ridgepoles
			//		   on a.BuildingId equals c.RidgepoleId
			//		   join d in _context.Columnmanagements
			//		   on a.ColumnId equals d.ColumnManagementId
			//		   where a.IsDel==false
			//		   select a;
			var list=_context.Purchaseformmodels.Where(c=>c.IsDel==false).AsQueryable();
			return list;
		}
		/// <summary>
		/// 获取供应商列表
		/// </summary>
		/// <returns></returns>
		public async Task<IQueryable<Vendormodel>> GetVendormodel()
		{
			var list = _context.Vendormodels.AsQueryable();
			return list;
		}

		/// <summary>
		/// 获取栋舍列表
		/// </summary>
		/// <returns></returns>
		public async Task<IQueryable<Ridgepole>> GetRidgepole()
		{
			var list = _context.Ridgepoles.AsQueryable();
			return list;
		}

		/// <summary>
		/// 获取栏位列表
		/// </summary>
		/// <returns></returns>
		public async Task<IQueryable<Columnmanagement>> GetColumnManagement(int columnManagementId)
		{
			var list = _context.Columnmanagements.Where(c=>c.ColumnManagementId==columnManagementId).AsQueryable();
			return list;
		}
		/// <summary>
		/// 养只采购添加
		/// </summary>
		/// <param name="entity"></param>
		/// <returns></returns>
		public async Task<int> AddPurchaseForms(List<Purchaseformmodel> entity)
		{
			_context.Purchaseformmodels.AddRange(entity);
			return _context.SaveChanges();
		}
		/// <summary>
		/// 羊只采购删除
		/// </summary>
		/// <param name="loginId"></param>
		/// <param name="ids"></param>
		/// <returns></returns>
		public async Task<int> DelPurchaseForms(string loginName, params int[] ids)
		{
			if(ids.Length<=0)
			{
				return 0;
			}
			var pur = _context.Purchaseformmodels.Where(c => ids.Contains(c.PurchaseFormId));
			foreach(var item in pur)
			{
				item.IsDel = true;
				item.UpdateTime= DateTime.Now;
				item.UpdateBy = loginName;
			};
			return _context.SaveChanges();
		}
		/// <summary>
		/// 羊只转舍添加
		/// </summary>
		/// <param name="entity"></param>
		/// <returns></returns>
		public async Task<int> AddPurchaseFormModel(List<Sheeprescission> entity)
		{
			_context.Sheeprescissions.AddRange(entity);
			return _context.SaveChanges();
		}
		/// <summary>
		/// 反填羊只采购
		/// </summary>
		/// <param name="purchaseFormId"></param>
		/// <returns></returns>
		public async Task<Purchaseformmodel> Purchaseformmodel(int purchaseFormId)
		{
			var rntity= _context.Purchaseformmodels.FirstOrDefault(c=>c.PurchaseFormId == purchaseFormId);
			return rntity;
		}
		/// <summary>
		/// 修改养只采购
		/// </summary>
		/// <param name="entity"></param>
		/// <returns></returns>
		public async Task<int> UpdatePurchaseformmodel(Purchaseformmodel entity)
		{
			_context.Entry(entity).State = EntityState.Modified;
			_context.Update(entity);

			return await _context.SaveChangesAsync();
		}
	}
}
